package com.intct.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.regionserver.SteppingSplitPolicy;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025/6/12
 * 单例 饿汉式
 */
public class ConnectionHBaseUtils {

    // 空构造器
    private ConnectionHBaseUtils() {

    }

    // 属性
    private static Connection connection;

    // 静态代码块
    static {
        try {
            // 创建连接
            Configuration conf = new Configuration();
            conf.set("hbase.zookeeper.quorum", "jd-node:2181");
            connection = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取HBase连接
     * @return
     */
    public static Connection getConnection() {
        // 返回连接
       return connection;
    }

    /**
     * 关闭连接
     * @param connection
     */
    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
